class Solution:
    def addDigits(self, num: int) -> int:
        # while num > 9:
        #     num_str = str(num)
        #     num = sum([int(a) for a in num_str])
        # return num
        # 法二：模9运算
        if num == 0:
            return 0
        return 1 + ((num-1)) % 9
if __name__ == '__main__':
    s = Solution()
    print(s.addDigits(38))